const menuService = require('../service/SysMenuService')
const ResultUtil = require('../utils/ResultUtil')

/**
 * 业务控制层： 查询菜单列表
 * @param {*} req 
 * @param {*} res 
 */
const menuList = async (req, res) => {
  const param = req.query
  let result = null
  try {
    result = await menuService.selectMenuList(param)
    res.send(ResultUtil.success(200).add(result))
  } catch (e) {
    res.send(ResultUtil.error(500, e.message))
  }
}

/**
 * 业务控制层： 根据菜单id查询菜单信息
 * @param {*} req 
 * @param {*} res 
 */
const selectMenuById = async (req, res) => {
  console.log(req.params)
  const { id } = req.params
  try {
    const menu = await menuService.selectMenuById(id)
    res.send(ResultUtil.success(200, menu))
  } catch (error) {
    res.send(ResultUtil.error(500, error.message))
  }
}

/**
 * 业务控制层：添加菜单信息
 * @param {*} req 
 * @param {*} res 
 */
const addMenu = async (req, res) => {
  const menu = req.body
  menu.createdAt = new Date()
  console.log(menu)
  try {
    await menuService.addMenu(menu)
    res.send(ResultUtil.success(200, true))
  } catch (e) {
    res.send(ResultUtil.error(500, e.message))
  }

}

/**
 * 业务控制层：更新菜单信息
 * @param {*} req 
 * @param {*} res 
 */
const updateMenu = async (req, res) => {
  const menu = req.body
  menu.updatedAt = new Date()
  try {
    await menuService.updateMenu(menu)
    res.send(ResultUtil.success(208, true))
  } catch (error) {
    res.send(ResultUtil.error(500, error.message))
  }
}

/**
 * 业务控制层：删除菜单信息
 * @param {*} req 
 * @param {*} res 
 */
const deleteMenu = async (req, res) => {
  const { id } = req.body
  try {
    await menuService.deleteMenu(id)
    res.send(ResultUtil.success(204, true))
  } catch (error) {
    res.send(ResultUtil.error(500, error.message))
  }
}

module.exports = {
  menuList,
  selectMenuById,
  addMenu,
  updateMenu,
  deleteMenu
}